package com.wlcorp.forcazamis.web.action.storagein;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.sample.ISampleService;
import com.wlcorp.forcazamis.business.storagein.IStorageInService;
import com.wlcorp.forcazamis.domain.sample.SubSample;
import com.wlcorp.forcazamis.domain.storagein.StorageIn;
import com.wlcorp.forcazamis.domain.storagein.StorageInSample;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.Pagination;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.Validator;
import com.wlcorp.forcazamis.sys.util.WebUtil;

public class StorageInAction extends DispatchAction {
	
	public ActionForward storageInManager(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(new Staff());
		request.setAttribute("staffList",staffList);
		return mapping.findForward("manager");
	}
	
	public ActionForward getStorageInList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		StorageIn storageIn = new StorageIn();
		WebUtil.setRequestToObject(request,storageIn);
		IStorageInService service = ContextUtil.getInstance().getStorageInService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getStorageInCount(storageIn).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    storageIn.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<StorageIn> storageInList = service.getStorageInList(storageIn);
		request.setAttribute("storageInList",storageInList);
		return mapping.findForward("list");
	}
	
	public ActionForward getStorageIn(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		
		StorageIn storageIn = new StorageIn();
		WebUtil.setRequestToObject(request,storageIn);
		storageIn = ContextUtil.getInstance().getStorageInService().getStorageInById(storageIn.getStorageInId());
		if(null == storageIn){
			storageIn = new StorageIn();
		}
		request.setAttribute("storageIn",storageIn);
		return mapping.findForward("modify");
	}
	
	public ActionForward getStorageInDetail(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		
		StorageIn storageIn = new StorageIn();
		WebUtil.setRequestToObject(request,storageIn);
		storageIn = ContextUtil.getInstance().getStorageInService().getStorageInById(storageIn.getStorageInId());
		request.setAttribute("storageIn",storageIn);
		return mapping.findForward("modifyDetail");
	}
	
	public ActionForward selectSubSampleListTop(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		return mapping.findForward("selectSubSampleTop");
	}
	
	public ActionForward selectSubSampleList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		SubSample subSample = new SubSample();
		WebUtil.setRequestToObject(request,subSample);
		ISampleService service = ContextUtil.getInstance().getSampleService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getSubSampleCount(subSample).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(10);
	    pagination.generatePageJSPFragment(request, count);
	    subSample.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<SubSample> subSampleList = service.getSubSampleList(subSample);
		request.setAttribute("subSampleList",subSampleList);
		return mapping.findForward("selectSubSample");
	}
	
	public ActionForward modifyStorageIn(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		StorageIn storageIn = new StorageIn();
		String storageInId = request.getParameter("storageInId");
		if(Validator.checkValueIsValid(storageInId)){
			storageIn = ContextUtil.getInstance().getStorageInService().getStorageInById(Long.valueOf(storageInId));
		}else{
			storageIn.setSubmiter(SessionValue.getInstance(request).getStaff());
			storageIn.setInStorageDate(new Date(Calendar.getInstance().getTimeInMillis()));
			storageIn.setState(AppConstant.BILL_STATE_SUBMITED);
		}
		WebUtil.setRequestToObject(request,storageIn);
		ContextUtil.getInstance().getStorageInService().saveStorageIn(storageIn);
		request.setAttribute("storageIn",storageIn);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyStorageInDetail(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String[] subSampleIdArray = request.getParameterValues("subSampleId");
		String[] inCountArray = request.getParameterValues("inCount"); 
		String[] priceArray = request.getParameterValues("price"); 
		String[] totalPriceArray = request.getParameterValues("totalPrice"); 
		String[] memoArray = request.getParameterValues("memo");
		String storageInId = request.getParameter("storageInId");
		List<StorageInSample> storageInSampleList = new ArrayList<StorageInSample>();
		if(null != subSampleIdArray){
			for (int i = 0; i < subSampleIdArray.length; i++) {
				StorageInSample storageInSample = new StorageInSample();
				storageInSample.setSubSampleId(Long.valueOf(subSampleIdArray[i]));
				storageInSample.setInCount(Long.valueOf(inCountArray[i]));
				storageInSample.setPrice(Double.valueOf(priceArray[i]));
				storageInSample.setTotalPrice(Double.valueOf(totalPriceArray[i]));
				storageInSample.setMemo(memoArray[i]);
				storageInSample.setStorageInId(Long.valueOf(storageInId));
				storageInSampleList.add(storageInSample);
			}
		}
		ContextUtil.getInstance().getStorageInService().saveStorageInSample(Long.valueOf(storageInId),storageInSampleList);
		StorageIn storageIn = ContextUtil.getInstance().getStorageInService().getStorageInById(Long.valueOf(storageInId));
		request.setAttribute("storageIn",storageIn);
		return mapping.findForward("modifyDetail");
	}
	
	public ActionForward printStorageIn(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		StorageIn storageIn = new StorageIn();
		WebUtil.setRequestToObject(request,storageIn);
		storageIn = ContextUtil.getInstance().getStorageInService().getStorageInById(storageIn.getStorageInId());
		request.setAttribute("storageIn",storageIn);
		return mapping.findForward("print");
	}
	
	public ActionForward withDrawStorageIn(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String storageInId = request.getParameter("storageInId");
		StorageIn storageIn = ContextUtil.getInstance().getStorageInService().getStorageInById(Long.valueOf(storageInId));
		ContextUtil.getInstance().getStorageInService().withDrawStorageIn(storageIn);
		request.setAttribute("storageIn",storageIn);
		return mapping.findForward("modifyDetail");
	}
	
	public ActionForward deleteStorageIn(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Long storageInId = Long.valueOf(request.getParameter("storageInId"));
		StorageIn storageIn = ContextUtil.getInstance().getStorageInService().getStorageInById(storageInId);
		ContextUtil.getInstance().getStorageInService().deleteStorageIn(storageIn);
		return getStorageInList(mapping,form,request,response);
	}
}
